import request from '../../utils/request';
Page({
	data: {
		videoGroupList: [], // 导航标签数据
		navId: '',
		videoList: [],
		videoId: '',
		videoUpdateTime: [], // 记录所有已播放视频的播放进度
		trigger: false,
	},
	onLoad: async function (options) {
		// 判断用户是否登录
		let userInfo = wx.getStorageSync('userInfo');
		if (!userInfo) {
			wx.redirectTo({
				url: '/pages/login/login',
			});
			return;
		}

		let videoGroupListData = await request('/video/group/list');
		this.setData({
			videoGroupList: videoGroupListData.data.slice(0, 14),
			navId: videoGroupListData.data[0].id,
		});

		this.getVideoList(this.data.navId);
	},
	async getVideoList(navId) {
		let videoListData = await request('/video/group/', { id: navId });
		let index = 0;
		let videoList = videoListData.datas.map((item) => {
			item.id = index++;
			return item;
		});
		this.setData({
			videoList,
			trigger: false,
		});
		wx.hideLoading();
	},
	changeNav(e) {
		this.setData({
			videoList: [],
			navId: e.currentTarget.dataset.id >>> 0,
		});
		wx.showLoading({
			title: '正在加载中',
		});
		this.getVideoList(this.data.navId);
	},
	handlePlay(e) {
		// 创建控制video标签的实例
		// 利用this的方式将实例对象绑定到当前页面实例上，单例模式应用
		// this.vid !== e.currentTarget.id && this.videoContext && this.videoContext.stop();
		this.vid = e.currentTarget.id;
		// 更新视频id
		this.setData({
			videoId: this.vid,
		});
		this.videoContext = wx.createVideoContext(this.vid);
		let videoObj = this.data.videoUpdateTime.find((item) => item.vid === this.vid);
		if (videoObj) {
			this.videoContext.seek(videoObj.currentTime);
		}
		this.videoContext.play();
	},
	handleTimeUpdate(event) {
		let { videoId, videoUpdateTime } = this.data;
		let currentVideoObj = { vid: videoId, currentTime: event.detail.currentTime };
		let videoTimeObj = videoUpdateTime.find((item) => item.vid === videoId);
		if (videoTimeObj) {
			// 之前保存过，所以在之前的基础上更新时间
			videoTimeObj.currentTime = event.detail.currentTime;
			// 不能放上面，没有重新设置，需要放下面
			// 判断如果视频完整播放完毕，自动将存在 videoUpdateTime 中的播放记录的对象移除
			if (videoTimeObj.currentTime === event.detail.duration) {
				videoUpdateTime.splice(
					videoUpdateTime.findIndex((item) => item.vid === videoTimeObj.vid),
					1
				);
			}
		} else {
			// 如果没有保存过，则新增
			videoUpdateTime.push(currentVideoObj);
		}
		this.setData({
			videoUpdateTime,
		});
	},
	handleRefresher() {
		// 发送请求获取最新数据
		this.getVideoList(this.data.navId);
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function (res) {
		if (res.from === 'menu') {
			return {
				title: '我是右上角的转发',
				path: '/pages/video/video',
				imageUrl: '/static/images/nvsheng.jpg',
			};
		} else {
			return {
				title: '我是按钮的转发',
				path: '/pages/video/video',
				imageUrl: '/static/images/nvsheng.jpg',
			};
		}
	},
	toSearch(){
		wx.navigateTo({
			url: '/pages/search/search',
		})
	}
});
